package com.VisualDisp.QuadCalculateProject.VisualDisplay.GraphShell;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.QuadCurve2D.Double;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.VisualDisp.QuadCalculateProject.Calculations.Equations.BaseEquation;
import com.VisualDisp.QuadCalculateProject.Calculations.Equations.Expressions;

public class LineDraw extends Thread implements EQDraw {

	protected double maxX,maxY,midX,midY,focalP1,focalP2;	
	protected Expressions equation;
	private Graphics2D graphics;
	private JPanel panel;
	
	private int diff;
	public LineDraw(Expressions exp)
	{

		this.equation = exp;
		focalP1 = equation.getResult().getX1();
		focalP2 = equation.getResult().getX2();
		this.setDaemon(true);
	}

	@Override
	public double calcNewX(double x) {
		// TODO Auto-generated method stub
		return (x + midX);
	}

	@Override
	public double calcNewY(double y) {
		// TODO Auto-generated method stub
		return (y + midY);
	}

	@Override
	public void draw() {
		this.start();
	}

	@Override
	public void run() {
		graphics.setColor(Color.RED);
		
		
		System.out.println("Diff:"+diff);		
		if(focalP1 == focalP2)
		{
			
			//go left from the focal point
			goLeft((int) focalP1);
			System.out.println("Finished Left");
			//go right from the focal point
			goRight((int)focalP2);
		
		}



		System.out.println("Finished");
	}

	private void goLeft(int focalP)
	{
		int myX = 0 , myY = 0;
		BaseEquation bs = equation.getEquation();
		Point temp = calcCordsX((int)focalP,(int)bs.calcY(focalP)),temp2; 
		for(int i = focalP ;  ; i--)
		{
			//t2 = i;
			myX = i;
			myY = (int)bs.calcY(myX);
			//Point p = calcCordsX(myX,myY);
			if(myY > maxY)// || p.getY() < 0)
			{
				myX = i + 1;
				myY = (int)bs.calcY(myX);
				break;
			
			}
		/*	System.out.println("cartX:"+myX+" , cartY:"+myY);
			temp = calcCordsX(myX,myY);
			System.out.println("pixX:"+temp.getX()+" ,pixY:"+temp.getY());
			t2--;
			temp2 = calcCordsX(t2,(int)(bs.calcY(t2)));
		//	graphics.drawLine(temp.x, temp.y, temp2.x, temp2.y);
			
			//graphics.fillOval((int)temp.getX(),(int)temp.getY(), 5, 5);
			*/
		}
		temp2 = calcCordsX(myX,myY);
		QuadCurve2D.Double curve = new QuadCurve2D.Double();
		curve.setCurve(temp.x,temp.y, focalP+midX, midY+bs.calcY(focalP), temp2.x, temp2.y);
		graphics.draw(curve);
	}
	
	private void goRight(int focalP)
	{
		int myX = 0 , myY = 0;
		BaseEquation bs = equation.getEquation();
		Point temp = calcCordsX(focalP, (int) bs.calcY(focalP)),temp2; 
		for(int i = focalP ;  ; i++)
		{
			
			myX = i;
			myY = (int)bs.calcY(myX);
			//Point p = calcCordsX(myX,myY);
			if(myY > maxY)// || p.getY() < 0)
			{
				//myX = i - 1;
				//myY = (int)bs.calcY(myX);
				break;
			
			}
			
			//System.out.println("piX:"+p.getX()+" ,piY:"+p.getY());
		/*	System.out.println("cartX:"+myX+" , cartY:"+myY);
			temp = calcCordsX(myX,myY);
			
			t2++;
			temp2 = calcCordsX(t2,(int)(bs.calcY(t2)));
			graphics.drawLine(temp.x, temp.y, temp2.x, temp2.y);*/
		}
		temp2 = calcCordsX(myX,myY);
		QuadCurve2D.Double curve = new QuadCurve2D.Double();
		System.out.println("pixX:"+temp.getX()+" ,pixY:"+temp.getY());
		System.out.println("pix2X:"+temp2.getX()+" ,pix2Y:"+temp2.getY());
		curve.setCurve(temp.x,temp.y, focalP+midX, midY+bs.calcY(focalP), temp2.x, temp2.y);
		graphics.draw(curve);
	}
	
	
	private Point calcCordsX(int x ,int y)
	{
		
		if(x > 0)
		{
			x = (int) (x+midX+diff);
			if(y >= 0)
			{
				y = (midY > y)?(int)(midY - y) : (int)(y - midY);
			}
			else
			{
				y = (int)(midY + y);
			}
		}
		else if( x == 0)
		{
			x = (int)(x + midX +24);
			if(y >= 0)
			{
				y = (int)(midY - y);
			}
			else
			{
				y = (int)(midY + y);
			}
		}
		else
		{
			x = (int)(midX + (x - diff));
			if(y >= 0)
			{
				y = (int)(midY - y);
			}
			else
			{
				y = (int)(midY + y);
			}
		}

		return new Point(x,y);

	}

	public void assignGraph(Graphics g2d) {
		this.graphics =(Graphics2D) g2d;

	}

	public void setDimensions(double d, double e) {
		this.maxX = d;
		this.maxY = e;
		this.midX = maxX/2;
		this.midY = maxY/2;
		this.diff = (int)(Math.sqrt(maxX)*2);
		System.out.println("Max"+maxX+"\nMaxy"+maxY);
		System.out.println("MidX"+midX+"\nMidY:"+midY);

	}

}
